package com.xiaoxiong.text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import android.util.Log;

import com.xiaoxiong.database.OperateFormSyllabus;


public class DealCourseData {
	private Context context;
	private String html;
	private String[] Time = new String[8];// 存上课时间
	private List<String> courseMess = new ArrayList<String>();// 存每节课信息
	private List<Map<String, String>> mapCourseMess = new ArrayList<Map<String, String>>();// 存分离好的课程信息
	private Map<String, String> CourseName = new HashMap<String, String>();// 存已处理过的课程名，以便处理颜色

	private int thisWeek;

	public DealCourseData(String html, Context context) {
		super();
		this.html = html;
		this.context = context;
		DealData();
	}

	public int getsv() {
		return thisWeek;
	}

	public void DealData() {
		Document content = Jsoup.parse(html);
		Elements element = content.getElementsByClass("font12");

		Elements element2 = content.getElementsByClass("style16");

		StringBuilder sb3 = new StringBuilder();
		for (Element links : element2) {
			String title = links.text();
			sb3.append(title);
			sb3.append("@");
		}
		int positionWeek = sb3.indexOf("第") + 1;
		this.thisWeek = sb3.charAt(positionWeek) - 48;
		char c = sb3.charAt(positionWeek + 1);
		if(c >= '0' && c <= '9'){
			this.thisWeek *= 10;
			this.thisWeek += (c - 48);
		}

		StringBuilder sb1 = new StringBuilder();
		for (Element links : element) {
			String title = links.text();
			sb1.append(title);
			sb1.append("@");
		}

		sb1.delete(0, sb1.indexOf("星期日") + 4);
		StringBuilder sbdeal = new StringBuilder();
		int flag = -1;
		int we = 1;
		for (int i = 0; i < sb1.length(); ++i) {
			if (sb1.charAt(i) == '@') {
				if (we != 8) {
					sbdeal.append("周" + we + "第" + flag + "节课\n");
					we++;
				}
			} else if (sb1.charAt(i) == '节') {
				sbdeal.append(sb1.charAt(i));
				we = 1;
				flag += 2;
			} else {
				sbdeal.append(sb1.charAt(i));
			}
		}

		// 先取出上课时间并去掉换行符
		StringBuilder afterReplace = new StringBuilder(GetTime(sbdeal)
				.toString().replace("\n", ""));
//		Log.d("afterReplace", afterReplace.toString());

		// 取出每节课信息
		GetCourse(afterReplace);

		// // 再分离课程信息
		serializeCourse();

		// 最后保存数据
		setCourseData();

	}

	// 保存数据
	public void setCourseData() {
		try {
			int id = 0;
			Map<String, String> mapcourse = new HashMap<String, String>();
			OperateFormSyllabus syllabus = new OperateFormSyllabus(context);
			syllabus.deleteAllSaysay();
			while (!mapCourseMess.isEmpty()) {
				mapcourse = mapCourseMess.get(0);
				mapCourseMess.remove(0);

//				Log.d("---", "coursename:" + mapcourse.get("coursename"));
//				Log.d("---", "time:" + mapcourse.get("time"));
//				Log.d("---", "teacher:" + mapcourse.get("teacher"));
//				Log.d("---", "room:" + mapcourse.get("room"));
//				Log.d("---", "weeks:" + mapcourse.get("weeks"));
//				Log.d("---", "week:" + mapcourse.get("week"));
//				Log.d("---", "starttime:" + mapcourse.get("starttime"));
//				Log.d("---", "color:" + mapcourse.get("color"));
//				Log.d("---", "---------------\n");
				
				
				syllabus.insertSyllabus(mapcourse.get("coursename"),
						mapcourse.get("time"), mapcourse.get("teacher"),
						mapcourse.get("room"), mapcourse.get("weeks"),
						Integer.parseInt(mapcourse.get("week")),
						Integer.parseInt(mapcourse.get("starttime")),
						Integer.parseInt(mapcourse.get("color")), id++);


			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 分离课程
	public void serializeCourse() {

		StringBuilder val = new StringBuilder();
		int color = 0;

		while (!courseMess.isEmpty()) {
			val.append(courseMess.get(0));
			courseMess.remove(0);

			StringBuilder coursename = new StringBuilder();// 课程名
			StringBuilder week = new StringBuilder();// 周几
			StringBuilder starttime = new StringBuilder();// 第几节
			StringBuilder teacher = new StringBuilder();// 老师名
			StringBuilder weeks = new StringBuilder();// 共几周
			StringBuilder room = new StringBuilder();// 教室

			int start = 0;
			int end = val.indexOf("(");
			StringBuilder name = new StringBuilder();
			for (int i = start; i < end; ++i) {
				name.append(val.charAt(i));
			}
			val.delete(0, end);

			String strName = name.toString().replace(" ", "").replace("周", "")
					.replace("第", " ").replace("节课", " ");
			name = new StringBuilder(strName);

			String[] amount = strName.split(" ");
			week.append(amount[0]);
			starttime.append(amount[1]);
			coursename.append(amount[2]);

			for (int i = val.indexOf("[") + 1; i < val.indexOf("]"); ++i) {
				room.append(val.charAt(i));
			}
			val.delete(0, 1);// 去掉左边的“（”
			val.delete(val.indexOf("周"), val.length());
			// 现在剩下教师名字跟周数
			for (int i = val.length() - 1; i > 0; --i) {
				char ch = val.charAt(i);
				if (ch != ' ' && (ch < '0' || ch > '9')) {
					start = i + 1;
					break;
				}
			}

			for (int i = start; i < val.length(); ++i) {
				weeks.append(val.charAt(i));
			}

			val.delete(start, val.length());
			teacher.append(val);
			val.delete(0, val.length());

			String time = getCourseTime(starttime);// 上课时间

			Map<String, String> map = new HashMap<String, String>();
			map.put("coursename", coursename.toString());
			map.put("week", week.toString());
			map.put("starttime", starttime.toString());
			map.put("teacher", teacher.toString());
			map.put("weeks", weeks.toString());
			map.put("room", room.toString());
			map.put("time", time);
			if (color == 0) {
				map.put("color", "0");
				CourseName.put(coursename.toString(), "0");
				color++;
			} else {
				boolean flag = false;
				for (String key : CourseName.keySet()) {
					if ((coursename.toString()).equals(key)) {
						flag = true;
						map.put("color", CourseName.get(key));
						break;
					}
				}
				if (!flag) {
					map.put("color", String.valueOf(color));
					CourseName
							.put(coursename.toString(), String.valueOf(color));
					color++;
				}
			}

			mapCourseMess.add(map);
		}
	}

	// 获取每节课的上课时间
	public String getCourseTime(StringBuilder starttime) {
		int pos = Integer.parseInt(starttime.toString());
		pos = ((pos + 1) / 2) - 1;
		return Time[pos];
	}

	// 取出每节课信息
	public void GetCourse(StringBuilder value) {
		StringBuilder val = new StringBuilder();
		String start;
		String end;
		int startInt;
		int endInt;
		for (int i = 1; i < 17; i += 2) {
			for (int j = 1; j < 8; ++j) {
				start = "周" + j + "第" + i + "节课";
				startInt = value.indexOf(start);
				if (i == 15 && j == 7) {
					endInt = value.length();
				} else {
					if (j == 7) {
						end = "周" + 1 + "第" + (i + 2) + "节课";
					} else {
						end = "周" + (j + 1) + "第" + i + "节课";
					}
					endInt = value.indexOf(end);
				}
				val.delete(0, val.length());
				for (int m = startInt; m < endInt; ++m) {
					val.append(value.charAt(m));
				}
				if (val.length() > 15) {
					int flag = 0;

					while (val.indexOf(",") != -1) {
						StringBuilder sbVal = new StringBuilder();
						if (flag != 0) {
							sbVal.append(start);
						}
						flag = 1;
						int position = val.indexOf(",");
						for (int k = 0; k < position; ++k) {
							sbVal.append(val.charAt(k));
						}
						val.delete(0, position + 1);

						String course = new String(sbVal);
						courseMess.add(course);
					}
					if (flag != 0) {
						val.insert(0, start);
						flag = 1;
					}
					String course = new String(val);
					courseMess.add(course);
				}
				value.delete(startInt, endInt);
			}
		}
	}

	// 取出时间
	public StringBuilder GetTime(StringBuilder sbdeal) {

		StringBuilder sbTime = new StringBuilder(sbdeal);

		int posend;// 时间的位置
		int posstart;
		posend = sbTime.indexOf("周1第1");

		Time[0] = new String(getTime(sbTime, 0, posend));
		sbTime.delete(0, posend);
//		Log.d("time", Time[0]);

		posstart = sbTime.indexOf("周7第1") + 7;
		posend = sbTime.indexOf("周1第3");
		Time[1] = new String(getTime(sbTime, posstart, posend));
		sbTime.delete(posstart, posend);

		posstart = sbTime.indexOf("周7第3") + 7;
		posend = sbTime.indexOf("周1第5");
		Time[2] = new String(getTime(sbTime, posstart, posend));
		sbTime.delete(posstart, posend);

		posstart = sbTime.indexOf("周7第5") + 7;
		posend = sbTime.indexOf("周1第7");
		Time[3] = new String(getTime(sbTime, posstart, posend));
		sbTime.delete(posstart, posend);

		posstart = sbTime.indexOf("周7第7") + 7;
		posend = sbTime.indexOf("周1第9");
		Time[4] = new String(getTime(sbTime, posstart, posend));
		sbTime.delete(posstart, posend);

		posstart = sbTime.indexOf("周7第9") + 7;
		posend = sbTime.indexOf("周1第11");
		Time[5] = new String(getTime(sbTime, posstart, posend));
		sbTime.delete(posstart, posend);

		posstart = sbTime.indexOf("周7第11") + 8;
		posend = sbTime.indexOf("周1第13");
		Time[6] = new String(getTime(sbTime, posstart, posend));
		sbTime.delete(posstart, posend);

		posstart = sbTime.indexOf("周7第13") + 8;
		posend = sbTime.indexOf("周1第15");
		Time[7] = new String(getTime(sbTime, posstart, posend));
		sbTime.delete(posstart, posend);

		return sbTime;
	}

	public String getTime(StringBuilder value, int start, int end) {
		StringBuilder val = new StringBuilder();

		for (int m = start; m < end; ++m) {
			val.append(value.charAt(m));
		}
		return val.toString();
	}

}
